package com.lwb.eduorder.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lwb.commonutils.CourseUtilsVo;
import com.lwb.commonutils.UcenterMemberVo;
import com.lwb.eduorder.client.EduSeviceClient;
import com.lwb.eduorder.client.UcenterMemberClient;
import com.lwb.eduorder.entity.TOrder;
import com.lwb.eduorder.mapper.TOrderMapper;
import com.lwb.eduorder.service.TOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lwb.eduorder.utils.OrderNoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-12-10
 */
@Service
public class TOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder> implements TOrderService {

    //远程调用
    @Autowired
    private EduSeviceClient eduSeviceClient;

    @Autowired
    private UcenterMemberClient ucenterMemberClient;

    //1. 生成订单方法
    @Override
    public String createOrder(String courseId, String jwtToken) {
        CourseUtilsVo courseInfo = eduSeviceClient.getCourseInfoByOrder(courseId);
        UcenterMemberVo userInfo = ucenterMemberClient.getUserInfo(jwtToken);

        //创建订单
        TOrder tOrder = new TOrder();
        tOrder.setOrderNo(OrderNoUtil.getOrderNo());
        tOrder.setCourseId(courseId);
        tOrder.setCourseTitle(courseInfo.getTitle());
        tOrder.setCourseCover(courseInfo.getCover());
        tOrder.setTotalFee(courseInfo.getPrice());
        tOrder.setTeacherName(courseInfo.getTeacherName());
        tOrder.setMemberId(userInfo.getMemberId());
        tOrder.setNickname(userInfo.getNickname());
        tOrder.setMobile(userInfo.getMobile());
        tOrder.setStatus(0);
        tOrder.setPayType(1);

        baseMapper.insert(tOrder);
        return tOrder.getOrderNo();
    }

    @Override
    public TOrder getOrderById(String orderNo) {
        QueryWrapper<TOrder> tOrderQueryWrapper = new QueryWrapper<>();
        tOrderQueryWrapper.eq("order_No",orderNo);
        TOrder tOrder = baseMapper.selectOne(tOrderQueryWrapper);
        return tOrder;
    }
}
